﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DNA.Mvc.Exporting.Exporters
{
    public class LanguageExporter : Exporter
    {
        public override string Name
        {
            get
            {
                return "langExporter";
            }
            set
            {
                base.Name = value;
            }
        }

        public override string Description
        {
            get
            {
                if (string.IsNullOrEmpty(base.Description))
                    return "Export the current language resources";

                return base.Description;
            }
            set
            {
                base.Description = value;
            }
        }

        private string resourceVPath = "~/App_GlobalResources/";

        public string ResVPath
        {
            get { return resourceVPath; }
            set { resourceVPath = value; }
        }

        public override void Execute(ExportingContext context)
        {
            var lang = context.Data[MatchKey];
            var fileList = new List<string>();
            var langPath = context.HttpContext.Server.MapPath(ResVPath);
            var dirs = Directory.GetDirectories(langPath);

            string dest = context.Destination + (context.Destination.EndsWith("\\") ? "" : "\\") + "content\\App_GlobalResources\\";

            foreach (var dir in dirs)
            {
                var lanResources = Directory.GetFiles(dir, string.Format("*.{0}.resx", lang));
                var dirInfo = new DirectoryInfo(dir);
                var destPath = dest + dirInfo.Name;
                if (!Directory.Exists(destPath))
                    Directory.CreateDirectory(destPath);
                
                var filename = "";
                var srcFileName = "";
                var destFileName = "";

                if (lanResources.Count() == 0)
                {
                    filename = (new DirectoryInfo(dir)).Name + ".resx";
                    srcFileName = dir + "\\" + filename;
                    destFileName = destPath + "\\" + filename;

                    if (File.Exists(srcFileName))
                        File.Copy(srcFileName, destFileName);
                }
                else
                {
                    for (int i = 0; i < lanResources.Count(); i++)
                    {
                        filename = Path.GetFileName(lanResources[i]);
                        srcFileName = dir + "\\" + filename;
                        destFileName = destPath + "\\" + filename;
                        if (File.Exists(srcFileName))
                            File.Copy(lanResources[i], destFileName);
                    }
                }
            }

            if (!context.Installers.ContainsKey("files"))
                context.Installers.Add("files", "");
        }
    }
}
